package com;

import com.util.boxs.BoxWorld;
import com.util.boxs.HitCallBack;
import com.util.boxs.bodys.BaseBody;
import com.util.boxs.bodys.RectBody;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 关卡内的所有可击溃物
 */
public class Part {
    private RectBody ground;
    private Wall[] walls = {};
    private Pig[] pigs = {};

    public Part(){
        ground = new RectBody(0,40,1300,40,true);
//      多米诺
//        wallList.add(Wall.getWood200(500,240));
//        wallList.add(Wall.getWood150(600,190));
//        wallList.add(Wall.getWood100(700,140));

        addWood(Wall.getWood200(500,240));
        addWood(Wall.getWood200(650,240));
        Wall wall = Wall.getWood200(570,350);
        wall.getBody().rotate(90);
        addWood(wall);

        addWood(Wall.getWood200(750,240));
        addWood(Wall.getWood200(900,240));
        wall = Wall.getWood200(820,350);
        wall.getBody().rotate(90);
        addWood(wall);

        addWood(Wall.getWood200(630,435));
        addWood(Wall.getWood200(780,435));
        wall = Wall.getWood200(700,545);
        wall.getBody().rotate(90);
        addWood(wall);



        addPig(Pig.getPig1(520,295));
        addPig(Pig.getPig2(720,63));
        addPig(Pig.getPig1(900,295));

//        addWood(Wall.getWood200(1200,240));
    }

    public void paintImage(Graphics2D g){
        for(int i=0;i< walls.length;i++){
            walls[i].paintImage(g);
        }
        for(int i=0;i< pigs.length;i++){
            pigs[i].paintImage(g);
        }

    }

    public void addWood(Wall wall){
        walls = Arrays.copyOf(walls,walls.length+1);
        walls[walls.length-1] = wall;
    }
    public void addPig(Pig pig){
        pigs = Arrays.copyOf(pigs,pigs.length+1);
        pigs[pigs.length-1] = pig;
    }

    public void outOfBoundsAction(){
        for(int i=0;i< walls.length;i++){
            if(walls[i].getBody().isOutOfBounds()){
                BoxWorld.remove(walls[i].getBody());
                walls[i] = walls[walls.length-1];
                walls = Arrays.copyOf(walls,walls.length-1);
            }
        }
        for(int i=0;i< pigs.length;i++){
            if(pigs[i].getBody().isOutOfBounds()){
                BoxWorld.remove(pigs[i].getBody());
                pigs[i] = pigs[pigs.length-1];
                pigs = Arrays.copyOf(pigs,pigs.length-1);
            }
        }
    }

    public void bangAction(){
        for(int i=0;i<pigs.length;i++){
            if(pigs[i].getHitBody().checkHit()){
//                BoxWorld.remove(pigs[i].getBody());
//                pigs[i] = pigs[pigs.length-1];
//                pigs = Arrays.copyOf(pigs,pigs.length-1);
                pigs[i].goDead();
            }
        }
    }
    public void deadAction(){
        for(int i=0;i<pigs.length;i++){
            if(pigs[i].isDead()){
                BoxWorld.remove(pigs[i].getBody());
                pigs[i] = pigs[pigs.length-1];
                pigs = Arrays.copyOf(pigs,pigs.length-1);
            }
        }
    }

    /**
     * 更新一下所有猪的碰撞环境，以便bangAction判断是否后续有新的物体碰撞
     */
    public void resetHit(){
        for(int i=0;i<pigs.length;i++){
            pigs[i].getHitBody().checkHit();
        }
    }
}
